#ifndef MOTIONTRACKER_H
#define MOTIONTRACKER_H

#include <OgreEntity.h>
#include "world.h"
#include "player.h"
#include "logger/logger.h"

#define MAX_GESTURE 800
#define SAMPLE_TIME 5
#define MAX_WALK 20

// motion track path setting
#define TRAIL_LENGTH 200
#define OFFSET 199
#define TRACKER_SIZE 0.12

// weight settings for low pass filter
#define WEIGHT_P1 0.65
#define WEIGHT_P2 0.22
#define WEIGHT_P3 0.11

#define X_VALUE 0
#define Z_VALUE 1

#define UNREACHABLE -1000

// color defines
#define COLOR_GREEN 1
#define COLOR_RED 2
#define COLOR_GREY 3

class World;
class Player;


class MotionTracker
{
public:
    MotionTracker(World *world, Player *player);
    void setTracker(Ogre::Vector3 position);
    void analyseGesture();
    void identifyGesture();
    void deleteMarkers();
    void deleteGrid();
    void deletePanels();
    void drawSquare(int squareNumber, int positionNumber,Ogre::Vector3 boxLocation,int color);
    bool inGesture(Ogre::Vector2 gesture[],int gestureLegth, Ogre::Vector2 errorPos);
    int getPositionInGesture(Ogre::Vector2 gesture[],int gestureLegth, Ogre::Vector2 errorPos);
    void drawPanel();
    void drawPanel(int gesture, bool training);

    bool inGridRectangle(int boxTop, int boxBottom, int boxLeft, int boxRight, Ogre::Vector3 pointOne, Ogre::Vector3 pointTwo);

    bool gridDrawn;
    bool m_panelsDrawn;

    Ogre::Entity* m_gesture[3];
    Ogre::SceneNode* m_gestNode[3];

    Ogre::Entity* m_rectangle[9];
    Ogre::SceneNode* m_recNode[9];

    Singleton *m_logger;

private:
    Ogre::Vector3 lowPassFilter(Ogre::Vector3 wiiRelPosition);
    void flush(int toDelete);
    void normalise();
    void gestureCleanup();
    void drawGrid(Ogre::Vector2 gesture[],int gestureLength, Ogre::Vector2 errorPos);
    int mod(int x, int m);

    Ogre::SceneNode* m_nodeArray[TRAIL_LENGTH];
    Ogre::Vector3 m_gesturePath[MAX_GESTURE];
    Ogre::Vector2 m_walk[MAX_WALK];

    Ogre::Vector3 m_prev_1;
    Ogre::Vector3 m_prev_2;
    Ogre::Vector3 m_prev_3;
    Ogre::Vector3 m_prev_4;

    Ogre::SceneManager *m_scene;
    Ogre::SceneNode *m_nodeTL,*m_nodeTM,*m_nodeTR,*m_nodeML,*m_nodeMM,*m_nodeMR,*m_nodeBL,*m_nodeBM,*m_nodeBR ;

    int m_time;
    int m_lastSampleTime;
    int m_gestureArrayIndex;
    int m_arrayIndex;
    int m_walkIndex;
    int m_follower;
    bool m_normalise;
    Player* m_player;

    World* m_world;
};

#endif // MOTIONTRACKER_H
